import BusinessConstant from '@/constant/BusinessConstant';
import request from '@/request';
import { ChatEntity } from '@/entities/ChatEntity';

const PREFIX = BusinessConstant.ROOT_SERVER + '/chat';

export default class ChatApi {
  /**
   * 添加一个聊天列表记录（会话记录）
   * @param senderId 发送者id（格式：T,1 或者 L,1）
   * @param receiverId 接收者id
   */
  static addChatSession(senderId: string, receiverId: string) {
    return request({
      method: 'POST',
      url: `${ PREFIX }/session`,
      data: {
        senderId,
        receiverId,
      }
    });
  }

  /**
   * 删除会话
   * @param senderId
   * @param receiverId
   */
  static delChatSession(senderId: string, receiverId: string) {
    return request({
      method: 'DELETE',
      url: `${ PREFIX }/session`,
      data: {
        senderId,
        receiverId,
      }
    });
  }

  /**
   * 获取会话列表
   * @param senderId
   */
  static getChatSession(senderId: string) {
    return request({
      method: 'GET',
      url: `${ PREFIX }/session?senderId=${ senderId }`
    });
  }

  /**
   * 添加一个聊天信息
   * @param data
   */
  static addChatMessage(data: ChatEntity.IAddChatMessage) {
    return request({
      method: 'POST',
      url: `${ PREFIX }/message`,
      data
    });
  }

  /**
   * 获取发送者和接收者之间的聊天记录
   * @param senderId
   * @param receiverId
   * @param page 第几页
   * @param limit 每页返回的个数
   */
  static getChatMessage(senderId: string, receiverId: string, page: number, limit: number) {
    return request({
      method: 'GET',
      url: `${ PREFIX }/message?senderId=${ senderId }&receiverId=${ receiverId }&page=${ page }&limit=${ limit }`
    });
  }

  /**
   * 获取会话列表的最新一条消息
   * @param senderId
   */
  static getChatSessionLastOneMessageList(senderId: string) {
    return request({
      method: 'GET',
      url: `${ PREFIX }/message/lastOne?senderId=${ senderId }`
    });
  }

  /**
   * 获取当前 sender 和 receiver 会话的最新消息
   * @param senderId
   * @param receiverId
   */
  static getCurrentChatSessionLastOneMessage(senderId: string, receiverId: string) {
    return request({
      method: 'GET',
      url: `${ PREFIX }/message/session/lastOne?senderId=${ senderId }&receiverId=${ receiverId }`
    });
  }

  /**
   * 离开会话
   * @param data
   */
  static leaveChatSession(data: { sessionId: number, senderId: string, receiverId: string }) {
    return request({
      method: 'PUT',
      url: `${ PREFIX }/session/leave`,
      data
    });
  }
  /**
   * 清除sender的获取消息的游标
   * @param senderId 
   * @returns 
   */
  static clearSenderSessionCursor(senderId: string) {
    return request({
      method: 'PUT',
      url: `${ PREFIX }/session/clearSenderCursor`,
      data: {
        senderId
      }
    })
  }
}